#include "GLTools.h"

uint32_t GLTools::compileVectexShader(const char* vectex_shader_source)
{
    uint32_t shader_id = 0;
    shader_id = glCreateShader(GL_VERTEX_SHADER);

    glShaderSource(shader_id, 1, &vectex_shader_source, nullptr);
    glCompileShader(shader_id);

    int success;
    char infoLog[512];
    glGetShaderiv(shader_id, GL_COMPILE_STATUS, &success);
    if (!success) {
        glGetShaderInfoLog(shader_id, 512, nullptr, infoLog);
        throw std::runtime_error(infoLog);
    }
    return shader_id;
}

uint32_t GLTools::compileFragmentShader(const char* fragment_shader_source)
{
    uint32_t shader_id = 0;
    shader_id = glCreateShader(GL_FRAGMENT_SHADER);

    glShaderSource(shader_id, 1, &fragment_shader_source, nullptr);
    glCompileShader(shader_id);

    int success;
    char infoLog[512];
    glGetShaderiv(shader_id, GL_COMPILE_STATUS, &success);
    if (!success) {
        glGetShaderInfoLog(shader_id, 512, nullptr, infoLog);
        throw std::runtime_error(infoLog);
    }
    return shader_id;
}

uint32_t GLTools::createProgram()
{
    return glCreateProgram();
}

void GLTools::bindAndLinkProgram(uint32_t shader_program, uint32_t vectex_shader, uint32_t fragment_shader)
{
    glAttachShader(shader_program, vectex_shader);
    glAttachShader(shader_program, fragment_shader);
    glLinkProgram(shader_program);

    int success;
    char infoLog[512];
    glGetProgramiv(shader_program, GL_LINK_STATUS, &success);
    if (!success) {
        glGetProgramInfoLog(shader_program, 512, nullptr, infoLog);
        throw std::runtime_error(infoLog);
    }
}

void GLTools::deleteShader(uint32_t& shader)
{
    glDeleteShader(shader);
    shader = 0;
}
